﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using System.Web;
using Antu.WorkFlowFactory;
using System.Data;

namespace WebSys.UI.OfficeFlow
{
    public class RollbackSkin : MPBasicTemplate
    {
        FlowFactoryProvider _FlowFactoryProvider = (FlowFactoryProvider)ProviderService.ProviderServiceMange.GetProvider("FlowFactoryProvider");
        string wiid, ctlId, iid, wid, userid, step;
        TextBox whyrollback;
        MPRepeater roolbackMPRepeater;
        StepType stepType;
        protected string UserId
        {
            get { if (base.Page.Session["UserId"] != null) { return base.Page.Session["UserId"].ToString(); } else { return string.Empty; } }
            set { base.Page.Session["UserId"] = value; }
        }
        protected override void OnLoad(EventArgs e)
        {
            LoginHelper.CheckLogin();
            InitControl();
            base.OnLoad(e);
            if (!base.Page.IsPostBack)
            {
                Initcy();
            }
        }

        private void InitControl()
        {
            wiid = base.Page.Request["wiid"];
            ctlId = HttpContext.Current.Server.UrlDecode(base.Page.Request["CTLID"]);
            iid = base.Page.Request["iid"];
            wid = _FlowFactoryProvider.GetWidByIID(iid);
            userid = UserId;
            step = _FlowFactoryProvider.GetStepName(ctlId,wid);

            roolbackMPRepeater = (MPRepeater)FindControl("roolbackMPRepeater");
            roolbackMPRepeater.ItemCommand += roolbackMPRepeater_ItemCommand;
            roolbackMPRepeater.ItemDataBound += roolbackMPRepeater_ItemDataBound;
            whyrollback = (TextBox)FindControl("whyrollback");

            bool canopen = _FlowFactoryProvider.CanOpen(userid, iid, wiid, step);
            bool isopen = _FlowFactoryProvider.IsOpen(iid, wiid, step);
            //if (!canopen || isopen)
            //{
            //    Antu.Utilities.UtilitiesPage.AlertDialogClear("该业务已被处理或无权限处理！", "window.parent.closeDialog();");
            //}
            string msg = "";
            if (false == _FlowFactoryProvider.TaskOpenCheck(iid, wiid, step, userid, out msg))
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("alert('" + msg + "！');", "window.parent.closeDialog();");
                return;
            }
            else
            {
                stepType = _FlowFactoryProvider.GetStepTypeByStep(wid, ctlId);
                if (stepType != StepType.MutiFlowEnd)//多流向结束岗位不可设置
                {
                    _FlowFactoryProvider.SetOpened(userid, iid, wiid, step);
                }
            }
        }

        private void roolbackMPRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "rollback")
            {
                if (!string.IsNullOrEmpty(whyrollback.Text))
                {
                    string remark = whyrollback.Text;

                    string newuserid, newstep;
                    DropDownList rollbackstep = (DropDownList)e.Item.FindControl("rollbackstep");
                    if (!string.IsNullOrEmpty(rollbackstep.SelectedValue))
                    {
                        newstep = rollbackstep.SelectedItem.ToString();
                        newuserid = rollbackstep.SelectedValue.Substring(0, rollbackstep.SelectedValue.IndexOf("|"));
                        int i = newstep.IndexOf("(");
                        if (i != -1)
                        {
                            newstep = newstep.Substring(i + 1);
                        }
                        newstep = newstep.Substring(0, newstep.LastIndexOf(")"));
                        if (_FlowFactoryProvider.Untread(long.Parse(wiid), remark, iid, userid, step, newuserid, newstep) == true)
                        {
                            Antu.Utilities.UtilitiesPage.AlertDialog("成功回退流程！", "window.parent.reloadandclose();");
                        }
                        else
                        {
                            Antu.Utilities.UtilitiesPage.AlertDialog("流程回退失败！", "history.back();");
                        }
                    }
                    else
                    {
                        Antu.Utilities.UtilitiesPage.AlertDialog("无岗位可退！", "window.parent.closeDialog();");
                    }
                }
                else
                {
                    Antu.Utilities.UtilitiesPage.AlertDialog("回退原因必须填写！", "history.back();");
                }
            }
        }

        private void roolbackMPRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Button rollback = (Button)e.Item.FindControl("rollback");
            DropDownList rollbackstep = (DropDownList)e.Item.FindControl("rollbackstep");

            DataTable dt = _FlowFactoryProvider.GetBackStepByiid(iid, userid, step);
            //因为有USERID有重复的情况，导致value相同，现在后面加|数字来区分
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["userid"] = dt.Rows[i]["userid"].ToString() + "|" + i.ToString();
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                rollbackstep.DataSource = dt;
                rollbackstep.DataTextField = "经办人";
                rollbackstep.DataValueField = "userid";
                rollbackstep.DataBind();
            }
            else
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("无岗位可退！", "window.parent.closeDialog();");
            }
        }

        private void Initcy()
        {
            DataTable dt = _FlowFactoryProvider.GetInstanceRollBack(iid);

            roolbackMPRepeater.DataSource = dt;
            roolbackMPRepeater.DataBind();

        }


    }
}


